********************************************************************************
***																			 ***
***     				  PERU - WOMEN COVID19 PROJECT 						 ***
***						   PILOT FIRST ROUND SURVEY					         ***
***																			 ***
********************************************************************************


/*** Defines global
	 ***************************************************************************/
	
	

use "$inputs_bartik/census2017_sector_shares.dta", clear
gen dpto=substr(ubigeo,1,2)
order dpto, before(ubigeo)
sort ubigeo  sector1r4


expand 3
bysort ubigeo sector1r4: gen year=2019 if _n==1
bysort ubigeo sector1r4: replace year=2020 if _n==2
bysort ubigeo sector1r4: replace year=2021 if _n==3

keep dpto ubigeo sector1r4 share_ubigeo year
rename share_ubigeo share_L_dist

merge m:1 year sector1r4 using "$inputs_bartik/sector_enaho2020_2019.dta", gen(_m1)
keep if _m1==3
drop _m1
sort ubigeo sector1r4 year

gen input_national=share_L_dist*weighted_N_national
bysort ubigeo year: egen bartik_national=total(input_national)
drop input_national
duplicates drop ubigeo year, force
keep ubigeo year bartik_national
save "$outcomes_bartik/bartik.dta", replace



// bartick 2019 and all 2020 panel 
// this is a panel with the 2019 avg and the 2020 quarters
use "$inputs_bartik/census2017_sector_shares.dta", clear
gen dpto=substr(ubigeo,1,2)
order dpto, before(ubigeo)
sort ubigeo  sector1r4

keep dpto ubigeo sector1r4 share_ubigeo 
rename share_ubigeo share_L_dist

expand 4
gen quarter=. 

bysort ubigeo sector1r4: gen realYear=2019 if _n==1
bysort ubigeo sector1r4: replace quarter=. if _n==1

bysort ubigeo sector1r4: replace realYear=2020 if _n==2
bysort ubigeo sector1r4: replace quarter=1 if _n==2

bysort ubigeo sector1r4: replace realYear=2020 if _n==3
bysort ubigeo sector1r4: replace quarter=2 if _n==3

bysort ubigeo sector1r4: replace realYear=2020 if _n==4
bysort ubigeo sector1r4: replace quarter=3 if _n==4

merge m:1 sector1r4 realYear quarter using "$data_folder/sector_enaho_2019_all2020Quarters.dta", nogen keep(1 3)

sort ubigeo sector1r4 realYear

gen input_national=share_L_dist*weighted_N_national
bysort ubigeo realYear quarter: egen bartik_national=total(input_national)
drop input_national

keep bartik_national quarter ubigeo realYear
duplicates drop 
isid quarter ubigeo realYear, missok 
save "$data_folder/bartick_2019_all2020Quarters.dta", replace



//create version that just uses employment percentage changes 
use "$inputs_bartik/census2017_sector_shares.dta", clear
gen dpto=substr(ubigeo,1,2)
order dpto, before(ubigeo)
sort ubigeo  sector1r4

//from enahoCode/0_clean_enahos.do
//q2 relative to q1 2020
merge m:1  sector1r4 using "$data_folder/changes_employment_sector.dta", nogen

ren pct_chng_wgt pct_chng_wgtq1
//q2 2020 relative to all of 2019
merge m:1  sector1r4 using "$data_folder/changes_employment_sector_v2.dta", nogen
ren pct_chng_wgt pct_chng_wgt19

bysort ubigeo : egen bartikq2q1 =total(pct_chng_wgtq1*share_ubigeo)
bysort ubigeo : egen bartikq219 =total(pct_chng_wgt19*share_ubigeo)

keep ubigeo bartikq2q1 bartikq219
duplicates drop 
isid ubigeo 

save "$data_folder/bartick_emp_changes.dta", replace




// bartick 2019 and all 2020 panel 
// this is a panel with the 2019 avg and the 2020 quarters
use "$inputs_bartik/census2017_sector_shares.dta", clear

keep ubigeo sector1r4 share_ubigeo 
rename share_ubigeo share_L_dist

expand 7
gen quarter=. 

bysort ubigeo sector1r4: gen realYear=2019 if _n==1
bysort ubigeo sector1r4: replace quarter=1 if _n==1

bysort ubigeo sector1r4: replace realYear=2019 if _n==2
bysort ubigeo sector1r4: replace quarter=2 if _n==2

bysort ubigeo sector1r4: replace realYear=2019 if _n==3
bysort ubigeo sector1r4: replace quarter=3 if _n==3

bysort ubigeo sector1r4: replace realYear=2019 if _n==4
bysort ubigeo sector1r4: replace quarter=4 if _n==4

bysort ubigeo sector1r4: replace realYear=2020 if _n==5
bysort ubigeo sector1r4: replace quarter=1 if _n==5

bysort ubigeo sector1r4: replace realYear=2020 if _n==6
bysort ubigeo sector1r4: replace quarter=2 if _n==6

bysort ubigeo sector1r4: replace realYear=2020 if _n==7
bysort ubigeo sector1r4: replace quarter=3 if _n==7

// From ./01 a_ stack enahos.do
merge m:1 sector1r4 realYear quarter using "$data_folder/sector_enaho_2020_2019_quarterly_right_weights.dta", gen(test_merge)

sort ubigeo sector1r4 realYear

gen input_national=share_L_dist*weighted_N_national
bysort ubigeo realYear quarter: egen bartik_national=total(input_national)


drop input_national
keep bartik_national quarter ubigeo realYear 
duplicates drop 
isid quarter ubigeo realYear, missok 


save "$data_folder/bartick_2019allquarters_all2020Quarters.dta", replace

